﻿@{
    ViewBag.Title = "Index";
}

<script language="javascript">
    $(function () {
        var searchUrl = '@Url.Action("GetActiveActivities")';
        var edirUrl = '@Url.Action("Edit")'
        var startActivityUrl = '@Url.Action("Start")'
        var gridName = 'grid'

        $("#" + gridName).jqGrid({
            datatype: "json",
            url: searchUrl,
            mtype: 'GET',
            rowNum: 10,
            rowList: [10, 20, 30],
            height: 275,
            sortname: 'Description',
            viewrecords: true,
            sortorder: "desc",
            colNames: ["id", "", "Nome", "Descrição", "canEdit", "canStart", "isStarted"],
            colModel: [{ name: "Id", width: 1, hidden: true, key: true },
                       { name: "actions", width: 8, resizable: false, sortable: false, align: "center" },
                       { name: "Name", width: 50, sortable: false },
                       { name: "Description", width: 50, sortable: false },
                       { name: "canEdit", width: 1, hidden: true },
                       { name: "canStart", width: 1, hidden: true },
                       { name: "isStarted", width: 1, hidden: true }
            ],
            autowidth: true,
            gridComplete: function () {
                var rows = $("#grid").jqGrid('getRowData');

                for (var i = 0; i < rows.length; i++) {
                    var row = rows[i];
                    var actions = "";

                    if (row['canStart'] == "true") {
                        var address = startActivityUrl + "/" + row['Id'];
                        editButton = "<button class='button-icon-play-icon-only' onclick=\"window.location.href = '" + address + "'\">Editar</button>";
                        actions = editButton;
                    }
                    else if (row['isStarted'] == "true")
                    {
                        var address = edirUrl + "/" + row['Id'];
                        editButton = "<button class='button-icon-pause-icon-only' onclick=\"window.location.href = '" + address + "'\">Iniciar</button>";
                        actions = editButton;
                    }

                    if (row['canEdit'] == "true") {
                        var address = edirUrl + "/" + row['Id'];
                        editButton = "<button class='button-icon-edit-icon-only' onclick=\"window.location.href = '" + address + "'\">Editar</button>";
                        actions += editButton;
                    }

                    $("#grid").jqGrid('setRowData', row['Id'], { actions: actions });
                }

                environment.refreshStyles();
            }
        });
    });
</script>

<div class="content-left">
    @Html.Partial("ContentLeftPartial")
</div>
<div class="content-right">
    <h2>Index</h2>
    <table id="grid"></table>
    <p>&nbsp;</p>
</div>
